package com.mall.project.gs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mall.common.core.asserts.ApiAssert;
import com.mall.common.core.cons.StatusCons;
import com.mall.common.core.enums.ErrorCodeEnum;
import com.mall.common.core.model.response.ApiResponses;
import com.mall.common.core.model.supers.SuperController;
import com.mall.common.security.annotation.PreAuth;
import com.mall.project.gs.model.dto.GoodsSpuDTO;
import com.mall.project.gs.model.entity.GoodsSpu;
import com.mall.project.gs.model.parm.GoodsSpuPARM;
import com.mall.project.gs.service.IGoodsSpuService;
import com.mall.project.info.service.IPhotoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2021-10-11
 */
@Api(tags = {"商品信息"})
@RestController
@RequestMapping("/goodsSpu")
public class GoodsSpuRestController extends SuperController {
    @Autowired
    private IGoodsSpuService goodsSpuService;
    @Autowired
    private IPhotoService photoService;

    @PreAuth
    @ApiOperation("查询商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "商品名称 模糊", paramType = "query"),
            @ApiImplicitParam(name = "shelf", value = "是否上架（1：上架；0：下架）", paramType = "query", type = "int"),
            @ApiImplicitParam(name = "delFlag", value = "是否显示（1：正常；0：删除）", paramType = "query", type = "int"),
    })
    @GetMapping("/page")
    public ApiResponses<IPage<GoodsSpuDTO>> page(@RequestParam(value = "name", required = false) String name,
                                                 @RequestParam(value = "shelf", required = false) Integer shelf,
                                                 @RequestParam(value = "delFlag", required = false) Integer delFlag) {
        LambdaQueryWrapper<GoodsSpu> wrapper = Wrappers.<GoodsSpu>lambdaQuery()
                .like(StringUtils.isNotBlank(name), GoodsSpu::getName, name)
                .eq(Objects.nonNull(shelf), GoodsSpu::getShelf, shelf)
                .eq(Objects.nonNull(delFlag), GoodsSpu::getDelFlag, delFlag);
        IPage<GoodsSpu> iPage = goodsSpuService.page(this.getPage(), wrapper);
        IPage<GoodsSpuDTO> dtoiPage = iPage.convert(e -> e.convert(GoodsSpuDTO.class));
        for (GoodsSpuDTO record : dtoiPage.getRecords()) {
            record.setPicList(photoService.listPhoto(record.getId(), "商品信息"));
        }
        return success(dtoiPage);
    }

    @PreAuth
    @ApiOperation("单条查询商品信息")
    @GetMapping("/{id}")
    public ApiResponses<GoodsSpuDTO> getById(@PathVariable("id") String id) {
        GoodsSpu entity = goodsSpuService.getById(id);
        ApiAssert.notNull(ErrorCodeEnum.BAD_SELECT_FAILURE, entity);
        GoodsSpuDTO dto = entity.convert(GoodsSpuDTO.class);
        dto.setPicList(photoService.listPhoto(dto.getId(), "商品信息"));
        return success(dto);
    }

    @PreAuth
    @ApiOperation("新增商品信息")
    @PostMapping
    public ApiResponses<Void> create(@RequestBody @Validated(value = GoodsSpuPARM.Create.class) GoodsSpuPARM gsp) {
        GoodsSpu goodsSpu = gsp.convert(GoodsSpu.class);
        ApiAssert.isTrue(ErrorCodeEnum.BAD_ADD_FAILURE, goodsSpuService.save(goodsSpu));
        return success();
    }

    @PreAuth
    @ApiOperation("修改分类")
    @PutMapping
    public ApiResponses<Void> updateById(@RequestBody GoodsSpuPARM gsp) {
        GoodsSpu update = gsp.convert(GoodsSpu.class);
        ApiAssert.isTrue(ErrorCodeEnum.BAD_UPDATE_FAILURE, goodsSpuService.updateById(update));
        return success();
    }

    @PreAuth
    @ApiOperation("删除商品信息")
    @DeleteMapping("/{id}")
    public ApiResponses<Void> deleteById(@PathVariable("id") String id) {
        //修改商品信息状态
        ApiAssert.isTrue(ErrorCodeEnum.BAD_DELETE_FAILURE, goodsSpuService.update(Wrappers.<GoodsSpu>lambdaUpdate()
                .set(GoodsSpu::getDelFlag, StatusCons.DISABLE).eq(GoodsSpu::getId, id)));
        return success();
    }

    @PreAuth
    @ApiOperation("修改商品信息状态")
    @PutMapping("/changeShelf")
    public ApiResponses<Void> changeStatus(@RequestBody @Validated(GoodsSpuPARM.ChangeShelf.class) GoodsSpuPARM gsp) {
        GoodsSpu goodsSpu = new GoodsSpu();
        goodsSpu.setShelf(gsp.getShelf());
        ApiAssert.isTrue(ErrorCodeEnum.BAD_UPDATE_FAILURE, goodsSpuService.update(goodsSpu, Wrappers.<GoodsSpu>lambdaQuery()
                .in(GoodsSpu::getId, gsp.getIdList())));
        return success();
    }
}

